package com.micai.springboot.redis.consumer;

import com.micai.springboot.service.SmsService;
import com.micai.springboot.service.impl.DingDongCloudConfig;
import com.micai.springboot.util.sms.VerificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/28 14:59
 * @Description: Redis消息消费者
 */
public class CustomerInfoSubscriber implements MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerInfoSubscriber.class);

    private static final SmsService SMS_SERVICE;

    /**
     * 初始化短信平台配置
     */
    static {
        DingDongCloudConfig config = new DingDongCloudConfig();
        config.setApiKey("867f24f3bbe5a1c9fd852d170343e155");
        SMS_SERVICE = VerificationUtil.createSmsService(config);
    }

    /**
     * 接收消息
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        LOGGER.info("Received >> " + message +  ", " + Thread.currentThread().getName());
        // 接收消息，调用发送短信的服务
        if (message != null) {
            String[] strings = message.toString().split("-");
            SMS_SERVICE.send(strings[0], strings[1]);
        }
    }
}
